﻿using System.Collections.Generic;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Net.Loader;
using Assets.Scripts.Tool;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Panel
{
    public class LoginTimeOutPanel : UIBase<LoginTimeOutPanel>
    {

        #region 组件

        public InputField UserNameField;
        public InputField PassWordField;
        public Button LoginButton;

        #endregion

        //委托事件
        public delegate void OnLoginTimeOutEvent(bool trigger);

        public static OnLoginTimeOutEvent OnLoginTimeOut;

        protected override UIType UIType
        {
            get { return UIType.Panel; }
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform, true);
            LoginButton.onClick.AddListener(OnLoginButtonClick);
            UserNameField.text = PlayerPrefs.GetString("Account");
            if (OnLoginTimeOut != null)
                OnLoginTimeOut(true);
        }

       

        /// <summary>
        /// 登入按钮的点击事件
        /// </summary>
        private void OnLoginButtonClick()
        {
            if (string.IsNullOrEmpty(UserNameField.text) || string.IsNullOrEmpty(PassWordField.text))
            {
                UIManager._Instance.ShowTipInfo("用户名或密码不能为空！！！");
                return;
            }
            string account = UserNameField.text;
            string md5psd = Md5Encript.GetMd5HashStr(PassWordField.text);
            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict.Add("account", account);
            dict.Add("password", md5psd);
            dict.Add("type", 1);
            NetManager.Instance.SendRequest(RequestType.Login, dict, () =>
            {
                StudentInfo info = NetReaderFactory.Instance().GetInfoByType<StudentInfo>() as StudentInfo;
                PlayerPrefs.SetString("Account", UserNameField.text);
                PlayerPrefs.SetString("Password", PassWordField.text);
                Exit();//将LoginPanel退出
            });
        }
           

        protected override void Update()
        {
            if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
            {
                Debug.Log("enter点击事件，，，，，");
                LoginButton.onClick.Invoke();
            }
        }

        protected override void InitOnDestroy()
        {
            if (OnLoginTimeOut != null)
            {
                OnLoginTimeOut(false);
            }
        }
    }
}
